package com.yihu.wlyy.web.common.version;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yihu.wlyy.entity.Versions;
import com.yihu.wlyy.service.app.version.VersionsService;
import com.yihu.wlyy.web.BaseController;

/**
 * 版本号控制类
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/version")
public class VersionController extends BaseController {

	@Autowired
	private VersionsService versionsService;

	/**
	 * 校验APP版本号
	 * @param code 版本类型编码
	 * @param version 当前版本号
	 * @return
	 */
	@RequestMapping(value = "app")
	@ResponseBody
	public String appVersion(String code, double version) {
		try {
			Versions temp = versionsService.findVersionByCode(code);
			if (temp == null) {
				return error(-1, "无效的APP类型失败！");
			}
			if (version > 0) {
				if (temp.getVersionInt() > version) {
					// 有新的版本号
					return write(200, "发现新版本！", "url", temp.getUrl());
				} else {
					// 已是最新版本
					return write(200, "已是最新版本！");
				}
			} else {
				JSONObject json = new JSONObject();
				json.put("version_int", temp.getVersionInt());
				json.put("version_str", temp.getVersionStr());
				json.put("url", temp.getUrl());
				json.put("info", temp.getInfo());
				json.put("size", temp.getSize());
				return write(200, "读取版本号成功！", "data", json);
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "版本号校验失败！");
		}
	}

}
